package com.lkn;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.util.Spliterator;

public class ATM {
    private ArrayList<Account> accounts=new ArrayList<>();
    private Scanner sc=new Scanner(System.in);
    private Account loginAccount=new Account();
    //启动ATM系统，展示欢迎界面
    public void start(){
        while (true) {
            System.out.println("*****欢迎进入到了ATM系统*****");
            System.out.println("        1.用户登录");
            System.out.println("        2.用户开户");
            System.out.println("请选择一项：");
            int command =sc.nextInt();
            switch (command){
                case 1:
                    //用户登录
                    login();

                    break;

                case 2:
                    //用户开户
                    createAccount();
                    break;
                default:
                    System.out.println("没有该操作");
            }
        }
    }

    //用户登录操作
    private void login(){
        System.out.println("****系统登录****");
        //1.先判断系统是否有账户
        if(accounts.size()==0){
            System.out.println("当前系统无任何账户，请先开户，再登录");
            return;
        }
        while (true) {
            //2.输入卡号
            System.out.println("请输入您的卡号");
            String cardId= sc.next();
            Account account=getAccountByCardID(cardId);
            if(account==null){
                System.out.println("您输入的卡号不存在");
            }else{
                while (true) {
                    //3.输入密码
                    System.out.println("请您输入登陆密码");
                    String password= sc.next();
                    if(account.getPassWord().equals(password)){
                        //3.登陆成功
                        loginAccount=account;
                        System.out.println("恭喜您，"+account.getUserName()+"登陆成功，您的卡号是"+account.getCardID());
                        showUserCommand();
                        return;//跳出当前登录方法，返回主页
                    }else{
                        System.out.println("密码错误");
                    }
                }
            }
        }

    }

    //登陆后操作界面
    private void showUserCommand(){
        while (true) {
            System.out.println("****您可以选择如下操作****");
            System.out.println("1.查询账户");
            System.out.println("2.存款");
            System.out.println("3.取款");
            System.out.println("4.转账");
            System.out.println("5.修改密码");
            System.out.println("6.退出");
            System.out.println("7.注销账户");
            int command=sc.nextInt();
            switch (command){
                case 1://查询
                    showLoginAccount();
                    break;

                case 2://存款
                    depositMoney();
                    break;

                case 3://取款
                    drawmoney();
                    break;
                case 4://转账
                    transferMoney();
                    break;
                case 5://修改密码
                    updataPassword();
                    return;
                case 6://退出
                    System.out.println("退出成功");
                    return;
                case 7://注销
                    if(deleteAccount()==true){
                        return;
                    }
                    break;
                default:
                    System.out.println("当前操作不存在");
            }
        }

    }

    //账户密码修改
    private void updataPassword() {
        System.out.println("****密码修改****");
        while (true) {
            System.out.println("请您输入当前密码：");
            String passWord=sc.next();
            if(loginAccount.getPassWord().equals(passWord)){
                //认证通过
                //开始修改
                while (true) {
                    System.out.println("请您输入新密码：");
                    String nextPassWord = sc.next();
                    System.out.println("请输入确认密码：");
                    String okPassWord = sc.next();
                    if(nextPassWord.equals(okPassWord)){
                        //两次密码相同
                        loginAccount.setPassWord(nextPassWord);
                        System.out.println("修改成功");
                        return;
                    }else{
                        System.out.println("两次密码不一致");
                    }
                }
            }else{
                System.out.println("您当前输入的密码不正确");
            }
        }
    }

    private boolean deleteAccount() {
        System.out.println("****销户操作****");
        System.out.println("确认销户吗?y/n");
        String command= sc.next();
        switch (command){
            case "y"://确认
                //判断账户余额为0
                if(loginAccount.getMoney()==0){
                    accounts.remove(loginAccount);
                    System.out.println("已成功销户");

                    return true;
                }else {
                    System.out.println("账户存在金额，不能销户");
                    return false;
                }

            default://保留
                System.out.println("取消");
                return false;
        }

    }

    //转账
    private void transferMoney() {
        System.out.println("****用户转账****");
        //1.判断是否存在其他账户
        if(accounts.size()<2){
            System.out.println("当前系统只有一个账户，不能转账");
            return;
        }
        //2.判断自己账户金额是否足够
        if(loginAccount.getMoney()==0){
            System.out.println("账户余额为0，不能转账");
            return;
        }
        while (true) {
            //3.输入对方账户
            System.out.println("请您输入对方卡号：");
            String cardId=sc.next();
            Account account=getAccountByCardID(cardId);
            if(account==null){
                System.out.println("对方账户不存在");
            }else{
                String name='*'+account.getUserName().substring(1);
                System.out.println("请输入【"+name+"】的姓氏");
                String prename=sc.next();
                if(account.getUserName().startsWith(prename)){
                    while (true) {
                        //成功认证
                        System.out.println("请您输入转账金额：");
                        double money=sc.nextDouble();
                        if(money<=loginAccount.getMoney()){
                            //成功转账
                            loginAccount.setMoney(loginAccount.getMoney()-money);
                            System.out.println("转账成功，转账金额:"+money+",当前余额："+loginAccount.getMoney());
                            account.setMoney(account.getMoney()+money);
                            return;
                        } else{
                            System.out.println("余额不足，当前余额："+loginAccount.getMoney());
                        }
                    }

                }else{
                    System.out.println("您输入的姓氏有误");
                }
            }
        }


    }

    //取钱
    private void drawmoney() {
        System.out.println("****取钱操作****");
        //先判断账户余额是否达到100元
        if(loginAccount.getMoney()<100){
            System.out.println("您的账户余额不足100元，不允许取钱");
            return;
        }

        while (true) {
            System.out.println("请输入您的取款金额");
            double money=sc.nextDouble();
            if(money<=loginAccount.getMoney()){
                //余额足
                if(money<=loginAccount.getLimit()){
                    loginAccount.setMoney(loginAccount.getMoney()-money);
                    System.out.println("恭喜您取款成功，取款金额为："+money);
                    System.out.println("卡内余额："+loginAccount.getMoney());
                    break;

                }else{//超出每次限额
                    System.out.println("超出每次取款限额，限额为："+loginAccount.getLimit());

                }

            }else{
                System.out.println("余额不足，您的余额是："+loginAccount.getMoney());

            }
        }
    }

    private void depositMoney() {
        System.out.println("****存钱操作****");
        System.out.println("请输入您的存款金额");
        double money=sc.nextDouble();
        loginAccount.setMoney(loginAccount.getMoney()+money);
        System.out.println("恭喜新存钱成功，余额为"+loginAccount.getMoney());

    }


    //展示当前登录的账户信息
    void showLoginAccount(){
        System.out.println("****您当前账户信息如下****");
        System.out.println("卡号："+loginAccount.getCardID());
        System.out.println("户主："+loginAccount.getUserName());
        System.out.println("性别："+loginAccount.getSex());
        System.out.println("余额："+loginAccount.getMoney());
        System.out.println("每日限额："+loginAccount.getLimit());


    }
    //用户开户操作
    private void createAccount(){
        System.out.println("****系统开户****");
        //1.创建一个用户对象
        Account account = new Account();

        //2.输入用户信息

        System.out.println("请输入您的账户名称：");
        String name=sc.next();
        account.setUserName(name);
        while (true) {
            System.out.println("请输入您的性别：");
            char sex=sc.next().charAt(0);
            if(sex=='男'||sex=='女')
            {
                account.setSex(sex);
                break;
            }else{
                System.out.println("您输入的性别有误");
            }
        }
        while (true) {
            System.out.println("请输入您的密码：");
            String password=sc.next();
            System.out.println("请再次输入您的密码");
            String okpassword=sc.next();
            if(password.equals(okpassword)){
                account.setPassWord(password);
                break;
            }else{
                System.out.println("您两次输入的密码不一致，请重新输入");

            }
        }

        System.out.println("请您输入您的取现额度：");
        double limit=sc.nextDouble();
        account.setLimit(limit);

        //随机生成8位卡号，卡号不能重复
        String cardId=createCardId();
        account.setCardID(cardId);
        
        //3.存入账户集合
        accounts.add(account);
        System.out.println("恭喜您，"+account.getUserName()+"开户成功，您的卡号是"+account.getCardID());

    }
    
    //返回一个八位数字的卡号
    private String createCardId(){
        while (true) {
            //1.定义一个String类型的变量存放卡号
            String cardId="";
            //2.通过循环生成卡号
            Random r=new Random();
            for (int i = 0; i <8 ; i++) {
                int data=r.nextInt(10);//0-9
                cardId+=data;
            }
            //3.判断当前生成的卡号是否已经存在
            Account account=getAccountByCardID(cardId);
            if(account==null){
                return cardId;
            }
        }

    }

    //根据卡号查找账户对象
    private Account getAccountByCardID(String CardId){
        //遍历全部的账户对象
        for (int i = 0; i < accounts.toArray().length; i++) {
            Account account=accounts.get(i);
            if(account.getCardID().equals(CardId)){
                return account;
            }

        }
        return null;
    }
}
